import Store from "../stores";
import $ from "jquery";
import {LoginType} from "../stores/GlobalStore";

export function CheckUserLogin() {
    /**
     * 判断用户是否登录
     */
    if (Store.userStore.userInfo) {
        return true;
    } else {
        Store.globalStore.updateShowLoginModalVisiable(true);
        return false;
    }
}

/**
 * 只判断是否登录，不弹登录框
 */
export function CheckUserLoginOnly() {
    if (Store.userStore.userInfo) {
        return true;
    } else {
        //Store.globalStore.updateShowLoginModalVisiable(true);
        return false;
    }
}

export function CheckUserLoginByThink() {
    Store.globalStore.SetLoginType(LoginType.THINK);
    if (Store.userStore.userInfo) {
        return true;
    } else {
        Store.globalStore.updateShowLoginModalVisiable(true);
        return false;
    }
}


export function CheckUserAuthorized() {
    if (!CheckUserLogin()) {
        return false;
    }
    // Store.userStore.checkBind
    return Store.userStore.bind;
}


export function ParseDate(date: Date) {
    return `${date.getFullYear()}-${date.getMonth()}-${date.getDay()} ${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}}`
}

/*修改显示的图片比例 1920*1000*/
export function ResetImageHeight() {
    setTimeout(() => {
        let imgParent = $("div#img-container");
        imgParent.each((index, element) => {
            let width = element.clientWidth;
            element.style.height = `${(width / 1920) * 1000}px `;
        })
    }, 0);
}

//随机字符名称
export function newGuid() {
    var guid = "";
    for (var i = 1; i <= 32; i++) {
        var n = Math.floor(Math.random() * 16.0).toString(16);
        guid += n;
        if ((i == 8) || (i == 12) || (i == 16) || (i == 20))
            guid += "-";
    }
    return guid;

}

/*
* 设置富文本内容
* */
export function setRichTextImage(star) {
    var head = "<head>"
        + "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\"> "
        + "<style>img{max-width: 100%; width:auto; height:auto;}</style>"
        + "<style>table{max-width: 100%; width:auto; height:auto;}</style>"
        + "</head>";
    return "<html>" + head + "<body>" + star + "</body></html>";
}

/*
* 检查用户信息更新
* */
export function CheckUserUpdate() {
    let user = Store.userStore;
    let local = localStorage.getItem("APP_ACCESSTOKEN");
    if (user.accessToken === local) {
        return;
    } else {
        user.setAccessToken(local);
    }
}

/*
* 检查用户是否有头像，没有给默认头像
* */
export function CheckUserHasAvatar() {
    if (Store.userStore.userInfo.avatarUrl) {
        return;
    } else {
        Store.userStore.userInfo.avatarUrl = "images/defaultAvatar.png";
    }
}

/*
* 默认头像地址
* */
export const defaultAvatar = "images/defaultAvatar.png";

/*
* 将秒数序列化
* */
export function FormatTime(time) {
    const h = Math.floor(time / 3600).toString().padStart(2, "0");
    const m = Math.floor((time / 60) % 60).toString().padStart(2, "0");
    const s = Math.floor(time % 60).toString().padStart(2, "0");
    return h + ":" + m + ":" + s;
}